package com.saifan.wyy_ov.ui.onlishop;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.CommonDataBean;
import com.saifan.wyy_ov.data.bean.PopCategoryBean;
import com.saifan.wyy_ov.data.bean.PopCategoryBean.Type;
import com.saifan.wyy_ov.data.bean.StoreBean;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.IGetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.RefreshLayout;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.DensityUtil;
import com.saifan.wyy_ov.utils.SoftUtil;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import lomasky.ma.xui.BaseRecyclerViewAdapter;
import lomasky.ma.xui.BaseViewHolder;
import lomasky.ma.xui.RecyclerViewAdapter;
import lomasky.ma.xui.WrapContentLineaLayoutManager;

/**
 * @author WangLi
 * @version V1.0
 * @date 2015年3月13日 下午5:53:15
 * @Description 周边商铺
 */

@SuppressLint({"InflateParams", "NewApi"})
public class AroundMallActivity extends BaseActivity implements SearchView.OnQueryTextListener {
    private ListView listView;
    private RelativeLayout loadlay;

    private int page = 0;// 第几页
    private CommonAdapter<StoreBean> adapter;
    private List<StoreBean> storeBeans;
    private IGetData getData;
    private CommonDataBean requestBean;
    //    private List<PopCategoryBean> popList, dataList;
    private PopupWindow popupWindow;
    private PopCategoryBean popCategoryBean;
    //    private TextView leftOperateTextView, rightOperateTextView;
//    private LinearLayout operateLayout, leftOperateLayout, rightOperateLayout;
//    private int leftPosition = 0, rightPosition = -1;
//    RecyclerViewAdapter<Type> adapterRight;
//    RecyclerViewAdapter<PopCategoryBean> adapterLeft;
    private String storeType;
    private SearchView searchView;

    private int type = 0;
    private RefreshLayout refreshLayout;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aroundmall);

        storeType = getIntent().getStringExtra("category");
        requestBean = new CommonDataBean();
        requestBean.setYXDID(getRoomInfoBean().getXMBS());
        findViews();
        getData = new GetData();
        getDataInit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.around_store, menu);
        // 对搜索监听
        searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        if (searchView != null) {
            searchView.setOnQueryTextListener(this);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    // 初始化控件
    @SuppressLint("InflateParams")
    private void findViews() {
        this.refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        this.listView = (ListView) findViewById(R.id.listView);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        loadlay = (RelativeLayout) findViewById(R.id.loading_lay);
        loadlay.setVisibility(View.VISIBLE);
//        operateLayout = (LinearLayout) findViewById(R.id.operate);
//        leftOperateLayout = (LinearLayout) findViewById(R.id.left_operate);
//        leftOperateTextView = (TextView) findViewById(R.id.left_operate_text);
//        leftOperateTextView.setText(storeType);
//        rightOperateTextView = (TextView) findViewById(R.id.right_operate_text);
//        leftOperateLayout.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                showSelectPop();
//            }
//        });
//        rightOperateLayout = (LinearLayout) findViewById(R.id.right_operate);
//        rightOperateLayout.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                showSelectType();
//            }
//        });
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // TODO Auto-generated method stub
                page = 0;
                refreshLayout.setRefreshing(true);
                getDataInit();
            }
        });

        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                // TODO Auto-generated method stub
                refreshLayout.setLoading(true);
                page++;
                getDataInit();

            }
        });
        storeBeans = new ArrayList<StoreBean>();
        adapter = new CommonAdapter<StoreBean>(AroundMallActivity.this, storeBeans, R.layout.store_item) {
            @Override
            public void convert(ViewHolderHelper helper, StoreBean item) {
                helper.setText(R.id.store_name, StringUtil.returnShow(item.getStoreName()));
                ImageView statePic = helper.getView(R.id.state_pic);
                if (item.isIsDelivery()) {
                    statePic.setVisibility(View.VISIBLE);
                } else {
                    statePic.setVisibility(View.GONE);
                }
                String date = "", time = "";
                if (!StringUtil.isNullOrEmpty(item.getBusinessDate())) {
                    date = item.getBusinessDate().replace(",", "至");
                }
                if (!StringUtil.isNullOrEmpty(item.getBusinessTime())) {
                    time = item.getBusinessTime().replace(",", "至");
                }
                helper.setText(R.id.store_time, date + " " + time);
                if (StringUtil.isNullOrEmpty(item.getDeliveryInstructions())) {
                    helper.setText(R.id.note, StringUtil.returnShow(String.valueOf(item.getDeliveryAmount())) + " 元起送");
                } else {
                    helper.setText(
                            R.id.note,
                            StringUtil.returnShow(String.valueOf(item.getDeliveryAmount())) + " 元起送/"
                                    + StringUtil.returnShow(item.getDeliveryInstructions()));
                }
                helper.setImageByUrl(R.id.store_img, item.getHeadPhoto());
            }
        };

        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Intent intent = new Intent(AroundMallActivity.this, StoreDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", storeBeans.get(arg2));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    // 初始化数据
    private void getDataInit() {

        requestBean.setPage(page);
        if (page < 1) {
            loadlay.setVisibility(View.VISIBLE);
        }
        if (storeType.equals("全部")) {
            requestBean.setStoreType("");
        } else {
            requestBean.setStoreType(storeType);
        }
        requestBean.setType(type);
        getData.getDataJson(AroundMallActivity.this, Api.StoreList.STORELIST, requestBean, "", new ResponseData() {

            @Override
            public void onSuccess(String json) {

                List<StoreBean> list = (new Gson()).fromJson(json, new TypeToken<List<StoreBean>>() {
                }.getType());
                if (list != null && list.size() > 0) {
                    if (page == 0) {
                        storeBeans = list;
                    } else {
                        storeBeans.addAll(list);
                    }
                    adapter.mDatas = storeBeans;
                    adapter.notifyDataSetChanged();

                } else {
                    if (page == 0) {
                        ToastUtil.showToast(AroundMallActivity.this, "暂无相关店铺信息");
                    } else {
                        ToastUtil.showToast(AroundMallActivity.this, "暂无更多相关店铺信息");
                        page--;
                    }
                }
                loadlay.setVisibility(View.GONE);
                refreshLayout.setRefreshing(false);
                refreshLayout.setLoading(false);


            }

            @Override
            public void onFailed(String str) {

                loadlay.setVisibility(View.GONE);
                refreshLayout.setRefreshing(false);
                refreshLayout.setLoading(false);

            }
        });
    }


//    @Override
//    public boolean onQueryTextChange(String arg0) {
//        return false;
//    }
//
//    @Override
//    public boolean onQueryTextSubmit(String arg0) {
//        SoftUtil.hideSoftInput(AroundMallActivity.this, searchView);
//        Intent intent = new Intent(AroundMallActivity.this, SearchActivity.class);
//        intent.putExtra("name", arg0);
//        startActivity(intent);
//        return true;
//    }


    @Override
    public boolean onQueryTextSubmit(String query) {
        SoftUtil.hideSoftInput(AroundMallActivity.this, searchView);
        Intent intent = new Intent(AroundMallActivity.this, SearchActivity.class);
        intent.putExtra("name", query);
        intent.putExtra("storeType", storeType);
        startActivity(intent);
        return true;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }
}
